package com.test.liuhb.test;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

import java.io.File;

/**
 * Created by liu.hb on 2018/1/3.
 */

public class NotificationTestActivity extends Activity implements View.OnClickListener{
    Button button,btnwidth,btndownload;
    Button btnMusic;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);
        button=(Button)findViewById(R.id.btn);
        btnMusic=(Button)findViewById(R.id.btnMusic);
        btnwidth=(Button)findViewById(R.id.btnwidth);
        btndownload=(Button)findViewById(R.id.btndownload);
        NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn:
                Intent intent =new Intent(this,RecyclerViewActivity.class);
                PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);
                NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                    Notification notification=new NotificationCompat.Builder(this)
                            .setContentTitle("有通知啦！")
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.download))
                            .setContentIntent(pi)
                            .setAutoCancel(true)
                            .setSound(Uri.fromFile(new File("/storage/sdcard0/BaiduNetdisk/Music/raw/beep.ogg")))//设置铃声
                            .setVibrate(new long[]{0,1000,3000,1000})//设置震动
                            .setLights(Color.RED,3000,1000)//控制手机LED灯显示
                            .setOngoing(true)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText("dasdasahsfjkahsfahskfhashfahfskafhkhasfkaskfasjfashfhiasgfjgasfgujhsgfjhgasgfddasd"))

                            .setPriority(NotificationCompat.PRIORITY_MAX)
                            .build();
                // .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.storage)))//设置图片
                //.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)设置系统默认铃声和震动
                //.setDefaults(NotificationCompat.DEFAULT_ALL)设置所有的默认值
                //.setContentText("ha hahhhh   hhh  hh  faafasasdasdda hh   hhhhhhaha  hahahh   ahahah")设置内容
                manager.notify(1,notification);
                break;
            case R.id.btnMusic:
                show2(btnMusic);
                break;
            case R.id.btnwidth:
                show(btnwidth);
                break;
            case R.id.btndownload:
                show3(btndownload);
                break;
        }
    }
    /**
     * 带音乐播放器的通知栏
     */
    public void show2(View v){
        Intent intent =new Intent(this,NotificationTestActivity.class);
        PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.download);
        RemoteViews rv = new RemoteViews(getPackageName(),R.layout.view_custom_button);
        rv.setTextViewText(R.id.tv,"仗剑走天涯");//修改自定义View中的歌名

        //修改自定义View中的图片(两种方法)
//        rv.setImageViewResource(R.id.iv,R.mipmap.ic_launcher);
        rv.setImageViewBitmap(R.id.iv, BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

        builder.setContent(rv);
        builder.setContentIntent(pi);
        builder.setAutoCancel(true);
        Notification notification = builder.build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(2,notification);

    }
    //按钮的点击方法
    public  void show(View v){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.download);
        builder.setContentTitle("郭嘉");//系统限制，可能不显示
        builder.setContentText("我们打袁绍吧");//系统限制，可能不显示
        builder.setDefaults(Notification.DEFAULT_ALL);
        //添加宽视图
        NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
        style.setBigContentTitle("十胜十败");
        //由手机屏幕像素决定显示多少
        style.addLine("第一是道胜");
        style.addLine("第二是义胜");
        style.addLine("第三是治胜");
        style.addLine("第四是度胜");
        style.addLine("第五是谋胜");
        style.addLine("第六是德胜");
        style.addLine("第七是仁胜");
        style.addLine("第八是明胜");
        style.addLine("第九是文胜");
        style.addLine("第十是武胜");
        style.setSummaryText("作者：郭嘉");//添加概要
        builder.setStyle(style);
        Notification n = builder.build();
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(3, n);
    }
    public  void show3(View v){
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.download);
        builder.setContentTitle("下载");
        builder.setContentText("正在下载");
        final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(4, builder.build());
        builder.setProgress(100,0,false);
        //下载以及安装线程模拟
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<100;i++){
                    builder.setProgress(100,i,false);
                    manager.notify(4,builder.build());
                    //下载进度提示
                    builder.setContentText("下载"+i+"%");
                    try {
                        Thread.sleep(50);//演示休眠50毫秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //下载完成后更改标题以及提示信息
                builder.setContentTitle("开始安装");
                builder.setContentText("安装中...");
                //设置进度为不确定，用于模拟安装
                builder.setProgress(0,0,true);
                manager.notify(4,builder.build());
//                manager.cancel(NO_3);//设置关闭通知栏
            }
        }).start();
    }
}
